﻿using AutoMapper;
using B.S.Orders.Domain;
using B.S.Orders.ErrorCord;
using B.S.Orders.Write.Api.Application.Command.ProcessCommand;
using MediatR;
using SqlSugar;

namespace B.S.Orders.Write.Api.Application.CommandHander.ProcessCommandHandler
{
    public class AddEquipmentCommandHandler : IRequestHandler<AddEquipmentCommand, APIResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public AddEquipmentCommandHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(AddEquipmentCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.success;
            result.Msg = "添加成功";

            var model = mapper.Map<Equipment>(request);

            await db.Insertable(model).ExecuteCommandAsync();

            return await Task.FromResult(result);
        }
    }
}
